/**
 * <version>3.4.4</version>
 */
const __config = require("./config/env");
const colorOption = require("./config/colorOption");
const eventBus = require("./utils/eventBus");
const http = require("./utils/http");
import { attrConfList } from "./api/um";


App({
  globalData: {
    token: null, // 用户 token
    userInfo: {}, // 用户信息
    config: __config, // 环境配置
    requestQueue: [], // 请求队列
    isLogging: false, // 是否正在登录
    isWxLogin: true, // 是否微信授权登录
    eventBus, // 全局事件总线
    colorOption, // 颜色配置
    windowInfo: null, // 窗口信息: rpxRatio rpx比值; pixelRatio 设备像素比; screenWidth	屏幕宽度，单位px; screenHeight	屏幕高度，单位px; windowWidth	可使用窗口宽度，单位px; windowHeight	可使用窗口高度，单位px; statusBarHeight	状态栏的高度，单位px; screenTop	窗口上边缘的y值
    windowHeight: null, // 窗口高度
    pixelRatio: null, // 设备像素比
    StatusBar: null, // 状态栏高度
    Custom: null, // 菜单按钮位置信息
    CustomBar: null, // 自定义菜单栏高度
    curDept: { text: "请选择" }, // 当前部门
    attrConfMap: {}, //属性设置
  },

  onLaunch: function () {
    this.checkAuth();
    this.updateManager();
    this.getSystemInfo();
  },

  // 检查用户授权状态
  checkAuth() {
    wx.getSetting({
      success: (res) => {
        console.log('wx.getSetting', res)
      },
    });
  },

  // 检查小程序更新
  updateManager() {
    const updateManager = wx.getUpdateManager();
    updateManager.onUpdateReady(() => {
      // 提示用户有新版本，是否更新
      wx.showModal({
        title: "更新提示",
        content: "新版本已经准备好，是否重启应用？",
        success: (res) => {
          if (res.confirm) {
            updateManager.applyUpdate();
          }
        },
      });
    });
  },

  // 获取系统信息
  getSystemInfo() {
    wx.setBackgroundColor({
      backgroundColor: '#ff0000', // 窗口的背景色为白色
    })

    let windowInfo = wx.getWindowInfo();
    const {screenHeight,windowHeight,statusBarHeight,pixelRatio,safeArea } = windowInfo;
    windowInfo.rpxRatio = windowInfo.screenWidth / 750
    windowInfo.tabBarHeight = Math.max(
      0,
      safeArea
        ? windowHeight - safeArea.bottom
        : screenHeight - windowHeight
    ) + 1 + 110 * windowInfo.rpxRatio;
    const custom = wx.getMenuButtonBoundingClientRect();
    const CustomBar = custom.bottom + custom.top - statusBarHeight;

    // 更新全局数据
    this.globalData = {
      ...this.globalData,
      windowInfo,
      StatusBar: statusBarHeight,
      Custom: custom,
      CustomBar,
    }
  },
  getAttrConfList() {
    attrConfList(res => {
      let attrConfMap = {};
      res.data.forEach(item => {
        attrConfMap[item.attrType] = attrConfMap[item.attrType] || [];
        attrConfMap[item.attrType].push(item);
      });

      // 更新全局数据
      this.globalData.attrConfMap = attrConfMap;
    })
  },
  // 初始化页面
  initPage() {
    http.getUserInfo()
    return new Promise((resolve, reject) => {
      const token = this.globalData.token || wx.getStorageSync("token");
      if (!token) {
        // 无 token，执行登录
        this.doLogin().then(resolve).catch(reject);
      } else {
        this.globalData.token = token
        this.globalData.isLogging = false;
        wx.checkSession({
          success: () => resolve("success"), // session_key 未过期，直接返回成功
          fail: () => this.doLogin().then(resolve).catch(reject), // session_key 过期，重新登录
        });
      }
    });
  },

  // 执行登录
  async doLogin() {
    http.getToken()
  },

  // 获取当前页面带参数的 URL
  getCurrentPageUrlWithArgs() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const {route, options} = currentPage;
    let urlWithArgs = `/${route}?`;
    for (let key in options) {
      const value = options[key];
      urlWithArgs += `${key}=${value}&`;
    }
    return urlWithArgs.slice(0, -1);
  },
});
